import React from 'react'
import { HashRouter, Route, Switch, Redirect } from "react-router-dom"
import Login from '../views/login/Login'
import NewsSandBox from '../views/sandbox/NewsSandBox'
import News from '../views/news/News'
import Detail from '../views/news/Detail'

export default function IndexRouter() {
  return (
    <HashRouter>
      {/* Switch只匹配其中一个路由 */}
      <Switch>
        <Route path='/login' component={Login} />
        {/* 游客系统 */}
        <Route path='/news' component={News} />
        <Route path='/detail/:id' component={Detail} />
        {/* 需登录访问 */}
        <Route path='/' render={() => {
          // 未登录则重定向到登录页面，否则返回新闻页面
          // localStorage.setItem("token","123")
          return localStorage.getItem("token") ? <NewsSandBox></NewsSandBox> : <Redirect to="/login" />
        }} />
      </Switch>
    </HashRouter>
  )
}
